package com.toe.forum.webapi.utils;

import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.StateEnum;
import com.toe.common.censor.ImgCensor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Component
@Slf4j
public class UploadUtils {
    //@Value()找到配置文件中的值读取出来并且赋值给dirPath变量
   // @Value("${dirPath}")
    private static String dirPath = "E://toe-img";

    /**
     * 文件类型数组
     */
    private static  String[] IMG_TYPES = {".jpg",".jpeg" ,".png",".gif" , ".raw" ,".helc" ,".webp"};
    private static  String[] VIDEO_TYPES = {".mp4" , ".flv" ,".f4v",".webm" ,".m4v",".mov", ".3gp" ,".3g2",".rm",".rmvb" , ".wmv" ,".avi" , ".asf" ,".mpg" , ".mpeg" ,".mpe" , ".div", ".dv" ,".divx" };

    private static  String IMG = "image";
    private static  String VIDEO = "video";

    /**
     * 处理多个文件
     * @param file
     * @return
     */
    public static Map<String,List<String>> upload(MultipartFile[] file){
        Map<String , List<String>> files = new HashMap<>();
        files.put(IMG , new ArrayList<>());
        files.put(VIDEO , new ArrayList<>());
        /**
         * 接收到的为一个文件数组
         */
        log.info("文件数目" + file.length);
        for (int i = 0; i < file.length; i++) {
            // 文件名
            String fileName = file[i].getOriginalFilename();
            log.info("旧文件名：{}" + fileName);
            // 后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件后缀名:{}" + suffixName);
            // 新文件名
            String newFileName = UUID.randomUUID() + suffixName;
            /**
             * 将文件分类并加入到Map
             */
            if (judgingFileType(suffixName).equals(IMG)){
                files.get(judgingFileType(suffixName)).add(newFileName);
                log.info("新图片文件名{}:"+ newFileName);
            }else{
                files.get(judgingFileType(suffixName)).add(newFileName);
                log.info("新视频文件名{}:"+ newFileName);
            }
            //准备保存图片的文件夹路径
            File dirFile = new File(dirPath);
            if (!dirFile.exists()){
                dirFile.mkdirs();//创建文件夹
            }
            File dest = new File(dirPath+ "/" + newFileName);
            try {
                file[i].transferTo(dest);
                String violationReason = ImgCensor.ImgCensor(dirPath+ "/" + newFileName);
                if (violationReason!=null){
                    dest.delete();
                    throw new ToeServiceException(StateEnum.ERR_FORUM_VIOLATION,"此图片"+violationReason);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return files;
    }

    /**
     * 判断文件类型
     * @param suffixName
     * @return
     */
    public static  String judgingFileType(String suffixName){
        for (int i = 0; i < IMG_TYPES.length; i++) {
            if (suffixName.equals(IMG_TYPES[i])){
                return IMG;
            }
        }
        return VIDEO;
    }

    /**
     *  处理单个文件
     * @param file
     * @return
     */
    public static String upload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        log.info("旧文件名：" + fileName);
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        log.info("文件后缀名：" + suffixName);
        // 新文件名
        String newFileName = UUID.randomUUID() + suffixName;
        //准备保存图片的文件夹路径
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();//创建文件夹
        }
        File dest = new File(dirPath+ "/" + newFileName);
        try {
            file.transferTo(dest);
            String violationReason = ImgCensor.ImgCensor(dirPath+ "/" + newFileName);
            if (violationReason!=null){
                dest.delete();
                throw new ToeServiceException(StateEnum.ERR_FORUM_VIOLATION,"此图片"+violationReason);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dirPath+ "/" + newFileName;
    }
}
